package service.impl;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

import common.impl.exception.ServiceException;

import net.sf.json.JSONObject;
import dao.interf.IClassesDao;
import dao.interf.IStudentDao;
import dao.pojo.Classes;
import dao.pojo.Student;
import dao.pojo.Teacher;
import model.MStudent;
import service.interf.IStudent;

public class StudentImpl implements IStudent{
	private IStudentDao studentDao;
	private IClassesDao classesDao;

	public void setStudentDao(IStudentDao studentDao) {
		this.studentDao = studentDao;
	}

	public void setClassesDao(IClassesDao classesDao) {
		this.classesDao = classesDao;
	}

	@Override
	public String login(MStudent model) {
		Student stu = studentDao.getByProperty("stuEmail" , model.getStuEmail());
		Map<String,Object> session = ActionContext.getContext().getSession();
		if (stu == null) {
			return "0";
		} else {
			if (stu.getStuPassword().equals(model.getStuPassword())) {

				session.put("stuId", stu.getStuId());
				session.put("stuEmail", stu.getStuEmail());
				session.put("stuName", stu.getStuName());

				Classes classes = stu.getClasses();
				if(null != classes) {
					session.put("className", classes.getClassName());
					Teacher teacher = classes.getTeacher();
					if (null != teacher) {
						session.put("teaId", teacher.getTeaId());
						session.put("teaName", teacher.getTeaName());
					}
				}
				return "1";

			} else {
				return "0";
			}
		}
	}

	public String getInfo() {
		JSONObject obj = new JSONObject();
		Map<String,Object> session = ActionContext.getContext().getSession();
		obj.put("stuId", session.get("stuId"));
		obj.put("stuEmail", session.get("stuEmail"));
		obj.put("stuName", session.get("stuName"));
		obj.put("className", session.get("className"));
		obj.put("teaId", session.get("teaId"));
		obj.put("teaName", session.get("teaName"));

		return obj.toString();
	}

	@Override
	public String register(MStudent model) {
		Student stu = new Student();
		stu.setStuEmail(model.getStuEmail());
		stu.setStuName(model.getStuName());
		stu.setStuPassword(model.getStuPassword());
		Classes classes = classesDao.getById(model.getClassId());
		if (classes == null) {
			throw new ServiceException("ERR-0031");
		} else {
			stu.setClasses(classes);
		}
		return studentDao.add(stu);
	}

	@Override
	public String getById(MStudent model) {
		Student stu = studentDao.getById(model.getStuId());
		if(null == stu) {
			throw new ServiceException("ERR-0011");
		}

		return createStudentObj(stu).toString();
	}

	@Override
	public String deleteById(Integer id) {
		studentDao.deleteById(id);
		return "1";
	}

	@Override
	public String edit(MStudent model) {
		Student stu = studentDao.getById(model.getStuId());
		if (null == stu) {
			throw new ServiceException("ERR-0011");
		}
		Classes classes = classesDao.getById(model.getClassId());
		if (classes == null) {
			throw new ServiceException("ERR-0031");
		} else {
			stu.setClasses(classes);
		}
		stu.setStuEmail(model.getStuEmail());
		stu.setStuName(model.getStuName());
		stu.setStuPassword(model.getStuPassword());

		studentDao.edit(stu);
		return "1";
	}

	private JSONObject createStudentObj(Student stu) {
		JSONObject obj = new JSONObject();

		obj.put("stuId", stu.getStuId());
		if(null != stu.getClasses())
			obj.put("classId", stu.getClasses().getClassId());
		obj.put("stuEmail", stu.getStuEmail());
		obj.put("stuName", stu.getStuName());
		return obj;
	}

}
